package com.ddone.movie.service.impl;

import com.ddone.movie.bean.Movie;
import com.ddone.movie.dao.MovieDao;
import com.ddone.movie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ddone
 * @date 2024/3/22-17:44
 */
@Service
public class MovieServiceImpl implements MovieService {
    private static Map<String, Movie> movieMap;
    @Autowired
    private MovieDao movieDao;
    private static final Map<String, Double> dataMap =new HashMap<String, Double>(){{
       put("夏洛特烦恼",17.0);
       put("花季雨季",16.0);
       put("罗马假日",15.0);
       put("一个人的武林",14.0);
       put("唐人街探案",13.0);
       put("三傻大脑宝莱坞",12.0);
       put("西游降魔篇",11.0);
       put("食神",90.0);
       put("大内密探凌凌漆",80.0);
       put("功夫",60.0);
       put("大话西游之仙履奇缘",50.0);
       put("大话西游之大圣娶亲",40.0);
       put("审死官",30.0);
       put("泰坦尼克号",20.0);
       put("肖生克的救赎",10.0);
    }};

    static {
        movieMap = new HashMap<>();
        dataMap.keySet().forEach(key->{
            String movieId = getMovieId();
            movieMap.put(movieId, new Movie(movieId, key, dataMap.get(key)));
        });
    }

    public static String getMovieId() {
        return UUID.randomUUID().toString().replace("-", "").toUpperCase(Locale.ROOT);
    }

    @Override
    public List<Movie> getAll() {
        return new ArrayList<>(movieMap.values());
    }

    @Override
    public Movie getMovieById(String movieId) {
        return movieMap.get(movieId);
    }

    @Override
    public void saveMovie(Movie movie) {
        String movieId = UUID.randomUUID().toString().replace("-", "");
        movie.setMovieId(movieId);
        movieMap.put(movie.getMovieId(), movie);
    }

    @Override
    public void updateMovie(Movie movie) {
        movieMap.put(movie.getMovieId(), movie);
    }

    @Override
    public void removeMovieById(String movieId) {
        movieMap.remove(movieId);
    }
}
